package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.ReportVenueDrawingAuditMapper;
import com.eastfair.venueservice.dto.ReportVenueDrawingAuditDTO;
import com.eastfair.venueservice.entity.ReportVenueDrawingAudit;
import com.eastfair.venueservice.service.ReportVenueDrawingAuditService;
import com.eastfair.venueservice.vo.ReportVenueDrawingAuditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸审核记录
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportVenueDrawingAuditServiceImpl extends SuperServiceImpl<ReportVenueDrawingAuditMapper, ReportVenueDrawingAudit> implements ReportVenueDrawingAuditService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportVenueDrawingAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportVenueDrawingAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());

        return R.successDef();
    }

    @Override
    public List<ReportVenueDrawingAuditVO> listVOByReportVenueDrawingCategoryAuditId(Long reportVenueDrawingCategoryAuditId) {
        ReportVenueDrawingAuditDTO param = new ReportVenueDrawingAuditDTO();
        param.setReportVenueDrawingCategoryAuditId(reportVenueDrawingCategoryAuditId);
        List<ReportVenueDrawingAudit> list = listReportVenueDrawingAudits(param);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(reportVenueDrawingAudit -> BeanUtil.toBean(reportVenueDrawingAudit, ReportVenueDrawingAuditVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportVenueDrawingAudit> listByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        ReportVenueDrawingAuditDTO param = new ReportVenueDrawingAuditDTO();
        param.setReportVenueSpaceNumber(reportVenueSpaceNumber);
        return listReportVenueDrawingAudits(param);
    }

    @Override
    public List<ReportVenueDrawingAudit> listByReportVenueDrawingCategoryAuditIds(List<Long> reportVenueDrawingCategoryAuditIds) {
        ReportVenueDrawingAuditDTO param = new ReportVenueDrawingAuditDTO();
        param.setReportVenueDrawingCategoryAuditIdList(reportVenueDrawingCategoryAuditIds);
        return listReportVenueDrawingAudits(param);
    }

    @Override
    public List<ReportVenueDrawingAudit> listReportVenueDrawingAudits(ReportVenueDrawingAuditDTO param) {
        QueryWrapper<ReportVenueDrawingAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ReportVenueDrawingAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(StrUtil.isNotBlank(param.getReportVenueSpaceNumber()),
                        ReportVenueDrawingAudit::getReportVenueSpaceNumber, param.getReportVenueSpaceNumber())
                .eq(param.getReportVenueDrawingCategoryAuditId() != null,
                        ReportVenueDrawingAudit::getReportVenueDrawingCategoryAuditId, param.getReportVenueDrawingCategoryAuditId())
                .in(param.getReportVenueDrawingCategoryAuditIdList() != null && !param.getReportVenueDrawingCategoryAuditIdList().isEmpty(),
                        ReportVenueDrawingAudit::getReportVenueDrawingCategoryAuditId, param.getReportVenueDrawingCategoryAuditIdList())
        ;
        return list(queryWrapper);
    }

    @Override
    public boolean saveReportVenueDrawingAudit(ReportVenueDrawingAuditDTO auditDTO) {
        ReportVenueDrawingAudit reportVenueDrawingAudit = BeanUtil.toBean(auditDTO, ReportVenueDrawingAudit.class);
        return save(reportVenueDrawingAudit);
    }

    @Override
    public boolean saveReportVenueDrawingAuditBatch(List<ReportVenueDrawingAuditDTO> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveReportVenueDrawingAudit);
        return true;
    }

    @Override
    public boolean removeByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        List<ReportVenueDrawingAudit> list = listByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }
}
